package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.dao.EsObjectDao;
import edu.dayu.mapper.ArticleMapper;
import edu.dayu.pojo.Article;
import edu.dayu.service.ArticleService;
import edu.dayu.vo.EsObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private EsObjectDao esObjectDao;

    @Override
    public PageInfo<Article> articleList(Article article) {
        PageHelper.startPage(article.getPageNum(),article.getPageSize());
        List<Article> articles = articleMapper.articleList(article);
        PageInfo<Article> articlePageInfo = new PageInfo<>(articles);
        return articlePageInfo;
    }

    @Override
    public void articleInsert(Article article) {
        article.setCreateTime(LocalDateTime.now().toString());
        articleMapper.articleInsert(article);
//        esObjectDao.save(beanUtil(article));
    }

    @Override
    public void articleUpdate(Article article) {
        articleMapper.articleUpdate(article);
        if (article.getStatus()==2)
        esObjectDao.save(beanUtil(article));
    }

    @Override
    public void articleDelete(Article article) {
        articleMapper.articleDelete(article);
        esObjectDao.delete(beanUtil(article));
    }

    public EsObject beanUtil(Article article){
        EsObject esObject = new EsObject();
        BeanUtils.copyProperties(article,esObject);
        esObject.setLink("社团新闻");
        esObject.setObjectId(article.getId());
        esObject.setTime(article.getCreateTime().toString());
        esObject.setId(article.getId().toString());
        return esObject;
    }


}
